<?php

namespace App\Service\Shop;

use App\Models\Shop\ShopModel;
use App\Service\BaseService;
use Illuminate\Support\Facades\DB;

class ShopService extends BaseService
{

    /**
     * @param object $params
     * @return array|void
     * @throws \Exception
     */
    public static function getShopListByArea(object $params)
    {
        try {
            $shopLists = ShopModel::query()
                             ->where(function ($query) use ($params) {
                                 $queryParams = [
                                     "province" => attributeGet($params, "provinceCodes", []),
                                     "city"     => attributeGet($params, "cityCodes", []),
                                     "district" => attributeGet($params, "areaCodes", [])
                                 ];
                                 $or          = false;
                                 collect($queryParams)->each(function ($codes, $key) use (&$query, &$or) {
                                     if ($or) {
                                         if (count($codes)) {
                                             $query->orWhereIn($key, $codes);
                                         }
                                     } else {
                                         if (count($codes)) {
                                             $query->whereIn($key, $codes);
                                             $or = true;
                                         }
                                     }
                                 });
                             })->when(
                    $params->onlyCount,
                    function ($query) {
                        return $query->select(["province", DB::raw("count(*) as total")])->groupBy("province")->get();
                    },
                    function ($query) {
                        return $query->select([
                            "id",
                            "shopCode",
                            "name",
                            "province",
                            "city",
                            "district",
                            "address",
                            "addressPrefix"
                        ])->get();
                    }
                );
            return [
                "total" => $shopLists->count(),
                "list"  => $shopLists->toArray()
            ];
        } catch (\Exception $exception) {
            handleKfException($exception);
        }
    }

    /**
     * 模糊查询门店
     * @param string $params
     * @param array $fields
     * @param bool $isAddress
     * @return array
     * @throws \Exception
     */
    public static function getShopListByLikeSearch(string $params, array $fields = ['*'], bool $isAddress = false): array
    {
        try {
            return ShopModel::query()
                ->when(
                    is_numeric($params),
                    function ($query) use ($params) {
                        $query->orWhere("shopCode", "like", "%{$params}%")
                            ->orWhere("id", "like", "%{$params}%");
                    },
                    function ($query) use ($isAddress, $params) {
                        $query->when(
                            !$isAddress, function ($query) use ($params) {
                            $query->where("name", "like", "%{$params}%");
                        }
                        )->when(
                                $isAddress, function ($query) use ($params) {
                                $query->where("address", "like", "%{$params}%")
                                    ->orWhere("addressPrefix", "like", "%{$params}%");
                            }
                            );
                    }
                )
                ->when(
                    $fields, function ($query) use ($fields) {
                    $query->select($fields);
                }
                )
                ->get()
                ->toArray();
        } catch (\Exception $exception) {
            handleKfException($exception);
        }
    }

}
